package com.angel.controller;

import com.angel.bean.ProductType;
import com.angel.service.OtherlogService;
import com.angel.service.ProductTypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/productTypeController")
public class ProductTypeController {

    @Resource
    private ProductTypeService productTypeService;
    @Autowired
    private OtherlogService otherlogService;


    /***
     *  更新
     * @param productType
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping("/updateProductType")
    @ResponseBody
    public Object updateProductType(ProductType productType) {
        Map<Object, Object> map = new HashMap<>();
        int result = productTypeService.update(productType);
        if (result != 0) {
            map.put("result", "success");
        }
        return map;
    }

    /***
     * 删除
     * @param productType
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping("/deleteProductType")
    @ResponseBody
    public Object deleteProductType(ProductType productType) {
        Map<Object, Object> map = new HashMap<>();
        productType.setState(0);
        int result = productTypeService.update(productType);
        if (result != 0) {
            map.put("result", "success");
        }
        return map;
    }

    /**
     * 添加
     *
     * @RequiresPermissions("user") 权限控制器必须登录才能访问
     * @return 重定向所有员工
     */
    @RequiresPermissions("user")
    @RequestMapping("/addProductType")
    @ResponseBody
    public Object addProductType(ProductType productType) {
        Map<Object, Object> map = new HashMap<>();
        productType.setState(1);

        int result = productTypeService.insert(productType);
        if (result != 0) {
            map.put("result", "success");
        }
        return map;
    }

    /**
     * 查询单个
     *
     * @RequiresPermissions("user") 权限控制器必须登录才能访问
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping("/findOneProductType")
    @ResponseBody
    public Object findOneProductType(ProductType productType) {
        Map<Object, Object> map = new HashMap<>();
        ProductType proType = productTypeService.findOne(productType);
        if (proType != null) {
            map.put("proType", proType);
        }
        return map;
    }

    /**
     * 查询商品信息
     *
     * @RequiresPermissions("user") 权限控制器必须登录才能访问
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping("/findProductType")
    public String findProductType(@RequestParam(value = "pn", defaultValue = "1") Integer pn, Model model) {

        PageHelper.startPage(pn, 5);
        ProductType productType = new ProductType();
        productType.setState(1);
        List<ProductType> productTypeList = productTypeService.findProductType(productType);

        PageInfo pageInfo = new PageInfo(productTypeList);
        if (pageInfo != null) {
            model.addAttribute("pageInfo", pageInfo);
        }
        return "productTypeEntry";
    }

}

